home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1993, 1994 Marc Parmet.
- * This file is part of the Macintosh port of GNU Emacs.
- *
- * GNU Emacs is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- */
-
- #if defined(THINK_C)
- #include <MacHeaders>
- #else
- #include <Types.h>
- #include <Memory.h>
- #include <Quickdraw.h>
- #include <Windows.h>
- #include <TextEdit.h>
- #include <Resources.h>
- #endif
-
- #include <GestaltEqu.h>
- #include <Aliases.h>
- #include <stdarg.h>
- #include "utime.h"
- #include "errno.h"
- #include "stdio.h"
- #include "sys/param.h"
- #include "sys/stat.h"
- #include "pwd.h"
- #include "grp.h"
- #include "unix-constants.h"
- #include "unix-types.h"
-
- typedef int (*funcptr)();
-
- int errno;
- char **environ;
- char *sys_errlist[NERR];
- int sys_nerr = NERR;
-
- #define define_unix_trap0(RETTYPE,CNAME) \
- RETTYPE CNAME(void) { \
- return (RETTYPE) unix_trap(unix_##CNAME); }
-
- #define define_unix_trap1(RETTYPE,CNAME,ARG1TYPE) \
- RETTYPE CNAME(ARG1TYPE a) { \
- return (RETTYPE) unix_trap(unix_##CNAME,a); }
-
- #define define_unix_trap2(RETTYPE,CNAME,ARG1TYPE,ARG2TYPE) \
- RETTYPE CNAME(ARG1TYPE a,ARG2TYPE b) { \
- return (RETTYPE) unix_trap(unix_##CNAME,a,b); }
-
- #define define_unix_trap3(RETTYPE,CNAME,ARG1TYPE,ARG2TYPE,ARG3TYPE) \
- RETTYPE CNAME(ARG1TYPE a,ARG2TYPE b,ARG3TYPE c) { \
- return (RETTYPE) unix_trap(unix_##CNAME,a,b,c); }
-
- static int
- unix_trap(int request,...)
- {
- int err;
- volatile int result;
- static int (*unix_entry)(int,int *,va_list);
- static char inited;
- va_list arglist;
- jmp_buf jbuf;
-
- if (!inited) {
- sys_errlist[0] = "no error";
- sys_errlist[EUNDOC] = "undocumented error";
- sys_errlist[ENOTDIR] = "no such folder or volume";
- sys_errlist[ERANGE] = "argument out of range";
- sys_errlist[EXDEV] = "EXDEV error";
- sys_errlist[EINTR] = "EINTR error";
- sys_errlist[ENOENT] = "no such file";
- sys_errlist[EACCES] = "access denied";
- sys_errlist[EBADF] = "bad file name";
- sys_errlist[EEXIST] = "file exists";
- sys_errlist[EEOF] = "end of file";
- sys_errlist[EDIRNE] = "directory not empty";
- sys_errlist[EPERM] = "permission denied";
- sys_errlist[EINVAL] = "invalid argument";
-
- err = Gestalt('uniX',(long *)&unix_entry);
- if (err) ExitToShell();
-
- inited = 1;
- }
-
- /* If this code is PPC based, then unix_entry will be a UPP.
- Otherwise we are 68k based, and unix_entry is either a UPP or ProcPtr. */
- va_start(arglist,request);
- #if defined(powerc)
- result = CallUniversalProc((UniversalProcPtr)unix_entry,unix_entry_ProcInfo,
- request,&errno,arglist);
- #else
- if (*(unsigned short *)unix_entry == _MixedModeMagic) {
- /* Mode switch will clobber a4? */
- if (setjmp(jbuf) == 0) {
- result = (*unix_entry)(request,&errno,arglist);
- longjmp(jbuf,1);
- }
- }
- else
- result = (*unix_entry)(request,&errno,arglist);
- #endif
- va_end(arglist);
- return result;
- }
-
- define_unix_trap1(int, close, int)
- define_unix_trap2(int, open, char *,int)
- define_unix_trap3(int, write, int,char *,int)
- define_unix_trap3(int, read, int,char *,int)
- define_unix_trap1(int, dup, int)
- define_unix_trap2(int, creat, char *,int)
- define_unix_trap1(int, unlink, char *)
- define_unix_trap3(int, lseek, int,int,int)
- define_unix_trap1(int, pipe, int *)
- define_unix_trap2(int, dup2, int,int)
- define_unix_trap2(int, execvp, char *,char **)
- define_unix_trap1(int, exit, int)
- define_unix_trap1(int, _exit, int)
- define_unix_trap2(funcptr, signal, int,funcptr)
- define_unix_trap1(int, raise, int)
- define_unix_trap2(int, kill, int,int)
- define_unix_trap3(int, ioctl, int,int,int *)
- define_unix_trap1(int, wait, int *)
- define_unix_trap3(int, wait3, int *,int,int)
- define_unix_trap1(int, alarm, int)
- define_unix_trap0(int, pause)
- define_unix_trap1(char *, getwd, char *)
- define_unix_trap1(int, chdir, char *)
- define_unix_trap1(int, sleep, int)
- define_unix_trap0(int, getpid)
- define_unix_trap0(int, getppid)
- define_unix_trap1(int, fflush, FILE *)
- define_unix_trap1(int, fclose, FILE *)
- define_unix_trap2(int, mkdir, char *,int)
- define_unix_trap1(int, rmdir, char *)
- define_unix_trap2(int, utime, char *,struct utimbuf *)
- define_unix_trap2(int, access, char *,int)
- define_unix_trap2(int, rename, char *,char *)
- define_unix_trap1(int, isatty, int)
- define_unix_trap2(int, chmod, char *,int)
- define_unix_trap2(int, chgrp, char *,int)
- define_unix_trap2(int, chown, char *,int)
- define_unix_trap2(int, stat, char *,struct stat *)
- define_unix_trap2(int, stat_spec, FSSpec *,struct stat *)
- define_unix_trap1(int, closedir, struct DIR *)
- define_unix_trap1(char *, getenv, char *)
- define_unix_trap3(int, readlink, char *,char *,int)
- define_unix_trap2(int, lstat, char *,struct stat *)
- define_unix_trap2(int, symlink, char *,char *)
- define_unix_trap1(FILE *, _iob, int)
- define_unix_trap1(struct DIR *,opendir, char *)
- define_unix_trap1(struct direct *,readdir,struct DIR *)
- define_unix_trap2(int, FSSpec2unixfn,FSSpec *,Handle *)
- define_unix_trap3(int, unixfn2FSSpec,char *,FSSpec *,int)
- define_unix_trap0(jmp_buf *,vfork1)
- define_unix_trap1(int, vfork2, int)
-
- void
- unix_nop(void)
- {
- static char x;
- if ((++x & 31) == 0) unix_trap(unix_unix_nop,0L);
- }
-
- void
- abort(void)
- {
- raise(SIGABRT);
- _exit(0);
- }
-
- int
- link(char *s,char *t)
- {
- errno = EUNDOC;
- return -1;
- }
-
- int
- mktemp(char *s)
- {
- static int n = 0;
- char *start,*end;
-
- start = s;
- while (1) {
- if (*start == 0) return -1;
- if (*start == 'X') break;
- ++start;
- }
-
- end = start;
- while (1) {
- if (*end != 'X') break;
- ++end;
- }
-
- sprintf(start,"%0*d",end-start,n++);
- return 0;
- }
-
- int
- tell(int fd)
- {
- return lseek(fd,0,1);
- }
-
- int
- getuid()
- {
- return 0;
- }
-
- int
- geteuid()
- {
- return 0;
- }
-
- int
- getegid()
- {
- return 0;
- }
-
- static struct passwd pw;
- static struct group gp;
- static char dir[MAXPATHLEN],name[MAXNAMLEN];
- static char unknown[] = "unknown";
-
- struct passwd *
- getpwuid(uid_t uid)
- {
- char **u;
- short err;
- FSSpec spec;
- int too_long;
-
- pw.pw_uid = uid;
- if (uid == 0) {
- // Home directory is expansion of ~/
- err = unixfn2FSSpec("~/",&spec,0);
- if (err) return 0L;
- err = FSSpec2unixfn(&spec,&u);
- if (err) return 0L;
- HLock(u);
- too_long = (strlen(*u) >= MAXPATHLEN);
- if (too_long) { DisposHandle(u); return 0L; }
- strcpy(dir,*u);
- DisposHandle(u);
- pw.pw_dir = dir;
-
- // Name is in System, maybe.
- u = GetResource('STR ',-16096);
- if (u == 0L)
- pw.pw_name = unknown;
- else {
- HLock(u);
- pstrcpy(name,*u);
- HUnlock(u);
- PtoCstr((unsigned char *)name);
- pw.pw_name = name;
- }
- }
- else {
- pw.pw_dir = unknown;
- *(uid_t *)name = uid;
- name[sizeof(uid_t)] = '\0';
- pw.pw_name = name;
- }
-
- return &pw;
- }
-
- struct passwd *
- getpwnam(char *usr)
- {
- char **u;
- short err;
- FSSpec spec;
-
- strcpy(dir,"~");
- strcat(dir,usr);
- err = unixfn2FSSpec(dir,&spec,0);
- if (err) return 0L;
- err = FSSpec2unixfn(&spec,&u);
- if (err) return 0L;
- HLock(u);
- if (strlen(*u) >= MAXPATHLEN) { DisposHandle(u); return 0L; }
- strcpy(dir,*u);
- DisposHandle(u);
-
- pw.pw_dir = dir;
- pw.pw_name = unknown;
- pw.pw_uid = 0;
-
- return &pw;
- }
-
- struct group *
- getgrgid(gid_t gid)
- {
- gp.gr_gid = gid;
- gp.gr_name = name;
- *(gid_t *)name = gp.gr_gid;
- name[sizeof(gid_t)] = '\0';
- return &gp;
- }
-
- struct group *
- getgrnam(char *name)
- {
- return 0L;
- }
-
- int
- umask()
- {
- return 0;
- }
-